package com.gxjtkyy.datadistributer.writer.rdbms.dao;

import com.gxjtkyy.datadistributer.writer.rdbms.entity.TargetInstitution;
import java.util.List;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service
public class QueryDao {
   private final JdbcTemplate jdbcTemplate;

   public QueryDao(@Qualifier("queryJdbcTemplate") JdbcTemplate jdbcTemplate) {
      this.jdbcTemplate = jdbcTemplate;
   }

   public List<TargetInstitution> queryRelativeGantries(String orgaCode) {
      String sql = "select t.ORGA_CODE,\n       t.ORGA_NAME,\n       t.ORGA_LEVEL,\n       (select tt.id\n          from toll_gantry tt\n         where tt.orga_code = t.ORGA_CODE\n           and tt.operation <> 3) general_id\n  from institution_all_current t\n where t.IS_DELETED = 0\n   and t.ORGA_LEVEL = 15\n start with orga_code = ?\nconnect by prior orga_code = parent_orga_code";
      return this.jdbcTemplate.query(sql, new BeanPropertyRowMapper(TargetInstitution.class), new Object[]{orgaCode});
   }

   public List<TargetInstitution> queryRelativeStations(String orgaCode) {
      String sql = "select t.ORGA_CODE,\n       t.ORGA_NAME,\n       t.ORGA_LEVEL,\n       (select tt.id\n          from TOLL_STATION tt\n         where tt.orga_code = t.ORGA_CODE\n           and tt.operation <> 3) general_id\n  from institution_all_current t\n where t.IS_DELETED = 0\n   and t.ORGA_LEVEL = 8\n start with orga_code = ?\nconnect by prior orga_code = parent_orga_code";
      return this.jdbcTemplate.query(sql, new BeanPropertyRowMapper(TargetInstitution.class), new Object[]{orgaCode});
   }
}
